package com.yudean.esb.service.impl.mds;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yudean.esb.common.ServiceConstant;
import com.yudean.esb.domain.erp.IdsPmProTech;
import com.yudean.esb.mapper.mds.IdsPmProTechMapper;
import com.yudean.esb.service.IdsPmProTechService;
import com.yudean.esb.util.PageUtil;
import com.yudean.esb.util.XmlUtil;
import com.yudean.esb.util.vo.Response;
import com.yudean.esb.util.vo.pageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IdsPmProTechServiceImpl extends ServiceImpl<IdsPmProTechMapper, IdsPmProTech> implements IdsPmProTechService {

    private IdsPmProTechMapper idsPmProTechMapper;
    @Autowired
    public void setIdsPmProTechMapper(IdsPmProTechMapper idsPmProTechMapper) {
        this.idsPmProTechMapper = idsPmProTechMapper;
    }

    /**
     *
     * @param id 通过TECHID进行查找
     * @return XML格式返回生产项目记录
     */
    @Override
    public String findById(String id) {
        IdsPmProTech idsPmProTech = idsPmProTechMapper.selectById(id);
        if(idsPmProTech == null){
            log.warn("通过项目ID获取生产项目记录findById(),id={},查询结果为空",id);
            return ServiceConstant.NO_DATA;
        }
        List<IdsPmProTech> lists = new ArrayList<>();
        lists.add(idsPmProTech);
        return XmlUtil.bean2Xml(new Response(ServiceConstant.SERVICE_PRO_TECH_DATA,ServiceConstant.PUBLISH_SERVICE,lists));
    }

    /**
     *
     * @param map 将查询条件封装成一个Map
     * @return XML格式返回生产项目记录
     */
    @Override
    public String findByMultiCondition(HashMap<String,Object> map) {
        List<IdsPmProTech> idsPmProTeches = idsPmProTechMapper.selectByMap(map);
        if (idsPmProTeches.size() == 0){
            log.warn("通过项目编码查询生产项目记录findXmlByProjectCode(),techProjectCode={},查询结果为空",map.get("TECHPROJECTCODE"));
            return ServiceConstant.NO_DATA;
        }
        return XmlUtil.bean2Xml(new Response(ServiceConstant.SERVICE_PRO_TECH_DATA,ServiceConstant.PUBLISH_SERVICE,idsPmProTeches));
    }

    /**
     *
     * @param ids 传入一个List集合
     * @return XML格式返回生产项目记录
     */
    @Override
    public String findByIds(List<String> ids) {
        List<IdsPmProTech> idsPmProTeches = idsPmProTechMapper.selectBatchIds(ids);
        if (idsPmProTeches == null){
            log.warn("根据多个项目ID获取生产项目记录findByIds(),lists={},查询结果为空",ids);
            return ServiceConstant.NO_DATA;
        }
        return XmlUtil.bean2Xml(new Response(ServiceConstant.SERVICE_PRO_TECH_DATA,ServiceConstant.PUBLISH_SERVICE,idsPmProTeches));
    }

    /**
     * 分页查询
     * @param pageNo 当前页面
     * @param pageSize 页面大小
     * @return XML datalist
     */
    @Override
    public String findByPage(String pageNo, String pageSize) {
        //当前页
        long current = PageUtil.handlePageNo(pageNo);
        //每页显示条数
        long size = PageUtil.handlePageSize(pageSize);
        Page<IdsPmProTech> page = new Page<>(current,size);
        idsPmProTechMapper.selectPage(page, null);
        List<IdsPmProTech> records = page.getRecords();
        //总数
        long total = page.getTotal();
        if(records.size() == 0){
            log.info("分页查询生产项目记录findByPage(),pageNo={},pageSize={},total={},查询结果为空",pageNo,pageSize,total);
            return ServiceConstant.NO_DATA;
        }
        log.info("分页查询生产项目记录findByPage(),pageNo={},pageSize={},total={}",pageNo,pageSize,total);
        return XmlUtil.bean2Xml(new pageResult(ServiceConstant.SERVICE_PRO_TECH_DATA,ServiceConstant.PUBLISH_SERVICE,total,current,size,records));
    }

    @Override
    public IdsPmProTech selectByMap(Map<String, Object> map) {
        QueryWrapper<IdsPmProTech> wrapper = new QueryWrapper<>();
        map.forEach(wrapper::eq);
        return idsPmProTechMapper.selectOne(wrapper);
    }
}
